﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using YAPACS.BusinessObjects.Managements.Exams;
using YAPACS.Organizations;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Technician;

public class GetAdvancedSearchView(ICurrentUserOrganization currentUserOrganization, IRepository<Institution, Guid> institutionRepository) : WorkflowBase, IGetAdvancedSearchView, ITransientDependency
{
    private readonly ICurrentUserOrganization _currentUserOrganization = currentUserOrganization;
    private readonly IRepository<Institution, Guid> _institutionRepository = institutionRepository;

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        WorkflowOutput workflowOutput = new WorkflowOutput { Success = true };
        const string key = "Filter";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is ExamFilter examFilter)
        {
            //根据ExamFilter.Code?获取高级查询视图
        }
        if (string.IsNullOrWhiteSpace(workflowOutput.View))
        {
            workflowOutput.View = await Extensions.GetResourceStringAsync("Resources.Technician.advanced-search.json");
        }
        var currInstitution = await _institutionRepository.GetAsync(_currentUserOrganization.InstitutionId.Value);
        List<Institution> list = (await _institutionRepository.GetListAsync()).OrderBy(e => e.Order).ThenBy(e => e.CreationTime).ToList();
        workflowOutput.Data = new
        {
            Institution = currInstitution.Code,
            ExamTimeRange = string.Join(",", [DateTime.Today.ToString("yyyy-MM-dd"), DateTime.Today.ToString("yyyy-MM-dd")])
        };
        workflowOutput.Content = new
        {
            InstitutionOptions = list.Select(e => new
            {
                label = e.Name,
                value = e.Code
            })
        };
        //默认返回3个月的时间范围
        return workflowOutput;
    }
}
